package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.BlogBackInfoDTO;
import com.jyh.blog.dto.BlogHomeInfoDTO;
import com.jyh.blog.enums.FilePathEnum;
import com.jyh.blog.service.BlogInfoService;
import com.jyh.blog.strategy.context.UploadStrategyContext;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.BlogInfoVO;
import com.jyh.blog.vo.WebsiteConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

import static com.jyh.blog.constant.OptTypeConst.UPDATE;

@Api(tags = "博客信息")
@RestController
public class BlogInfoController {
    @Autowired
    private BlogInfoService blogInfoService;

    @Autowired
    private UploadStrategyContext uploadStrategyContext;

    /**
     * 获取网站主页信息
     * @return
     */
    @ApiOperation("获取网站主页信息")
    @GetMapping("/")
    public Result<BlogHomeInfoDTO> getBlogHoneInfo(){
        return Result.ok(blogInfoService.getBlogHoneInfo());
    }

    /**
     * 访客记录
     * @return
     */
    @ApiOperation(value = "访客记录")
    @PostMapping("/report")
    public Result report(){
        blogInfoService.report();
        return Result.ok();
    }

    /**
     * 获取网站后台主页信息
     * @return
     */
    @ApiOperation(value = "获取网站后台主页信息")
    @GetMapping("admin")
    public Result<BlogBackInfoDTO> getBlogBackInfo(){
        return Result.ok(blogInfoService.getBlogBackInfo());
    }

    /**
     * 获取配置信息
     * @return
     */
    @ApiOperation(value = "获取配置信息")
    @GetMapping("admin/websiteConfig")
    public Result<WebsiteConfigVO> getWebSiteConfig(){
        return Result.ok(blogInfoService.getWebSiteConfig());
    }

    /**
     * 更新配置信息
     * @param websiteConfigVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "更新配置信息")
    @PutMapping("admin/websiteConfig")
    public Result<String> updateWebSiteConfig(@RequestBody WebsiteConfigVO websiteConfigVO){
        blogInfoService.updateWebSiteConfig(websiteConfigVO);
        return Result.ok();
    }

    /**
     * 修改关于我
     * @param blogInfoVO
     * @return
     */
    @ApiOperation("修改关于我")
    @PutMapping("admin/about")
    public Result updateAbout(@Valid @RequestBody BlogInfoVO blogInfoVO){
        blogInfoService.updateAbout(blogInfoVO);
        return Result.ok();
    }

    /**
     * 关于我
     * @return
     */
    @ApiOperation(value = "关于我")
    @GetMapping("about")
    public Result<String> getAbout(){
        return Result.ok(blogInfoService.getAbout());
    }

    /**
     * 上传配置图片
     * @param file
     * @return
     */
    @ApiOperation(value = "上传配置图片")
    @PostMapping("admin/config/images")
    public Result<String> savePhotoAlbumCover(MultipartFile file) {
        return Result.ok(uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.CONFIG.getPath()));
    }
}
